package com.coderman.business.service.imp;

import com.coderman.business.converter.StudentScoreConverter;
import com.coderman.business.mapper.StudentScoreMapper;
import com.coderman.business.service.IStudentScoreService;
import com.coderman.business.service.base.AbstractServiceImpl;
import com.coderman.common.error.SystemCodeEnum;
import com.coderman.common.error.SystemException;
import com.coderman.common.model.student.StudentScore;
import com.coderman.common.vo.student.StudentScoreVO;
import com.coderman.common.vo.system.PageVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * 学生考试信息业务层实现
 * @author  文晓平
 * @Date 2023-1-9
 * @version 1.0
 */
@Service
public class StudentScoreServiceImpl extends AbstractServiceImpl<StudentScoreMapper, StudentScore, Integer> implements IStudentScoreService {
   @Autowired
   private StudentScoreMapper studentScoreMapper;
    @Override
    public PageVO<StudentScore> findList(Integer pageNum, Integer pageSize, StudentScoreVO studentScoreVO) {
        PageHelper.startPage(pageNum,pageSize);
        Example o = new Example(StudentScore.class);
        StudentScore studentScore = new StudentScore();
        BeanUtils.copyProperties(studentScoreVO,studentScore);
        System.out.println("ffffffffffffffffffffffffffffffffff************userName"+studentScore.getStudentName());
        Example.Criteria criteria = o.createCriteria();
        o.setOrderByClause("examTime desc");

        if(studentScoreVO.getStartTime()!=null){
            criteria.andGreaterThanOrEqualTo("examTime",studentScoreVO.getStartTime());
        }
        if(studentScoreVO.getEndTime()!=null){
            criteria.andLessThanOrEqualTo("examTime",studentScoreVO.getEndTime());
        }
        List<StudentScore> studentScoreList = studentScoreMapper.selectStudentScoreList(studentScore);
        PageInfo<StudentScore> info=new PageInfo<>(studentScoreList);
        return new PageVO<>(info.getTotal(),studentScoreList);
    }

    /**
     * 添加学员成线
     *
     * @param studentScoreVO
     */
    @Override
    public void add(StudentScoreVO studentScoreVO) {
        StudentScore studentScore = new StudentScore();
        BeanUtils.copyProperties(studentScoreVO,studentScore);
        studentScoreMapper.insert(studentScore);
    }

    /**
     * 编辑学员成线
     *
     * @param id
     * @return
     */
    @Override
    public StudentScoreVO edit(Integer id) throws SystemException {
        StudentScore studentScore = studentScoreMapper.selectByPrimaryKey(id);
        if(studentScore==null){
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"编辑的学生成绩不存在");
        }
        StudentScoreVO studentScoreVO = StudentScoreConverter.converterToStudentScoreVO(studentScore);
        return studentScoreVO;
    }

    /**
     * 更新学员成线
     *
     * @param id
     * @param studentScoreVO
     */
    @Override
    public void update(Integer id, StudentScoreVO studentScoreVO) throws SystemException {
        StudentScore dbStudentScore = studentScoreMapper.selectByPrimaryKey(id);
        if(dbStudentScore==null){
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"编辑的学员成绩不存在");
        }
        StudentScore studentScore = new StudentScore();
        BeanUtils.copyProperties(studentScoreVO,studentScore);
        studentScore.setScoreId(id);
        studentScoreMapper.updateByPrimaryKeySelective(studentScore);

    }

    /**
     * 删除学员成绩
     *
     * @param id
     */
    @Override
    public void delete(Integer id) throws SystemException {
        StudentScore studentScore = studentScoreMapper.selectByPrimaryKey(id);
        if(studentScore==null){
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"要删除的学员成绩不存在");
        }
        studentScoreMapper.deleteByPrimaryKey(id);
    }

    /**
     * 所有学员成线
     *
     * @return
     */
    @Override
    public List<StudentScoreVO> findAllVO() {
        List<StudentScore> studentScores = studentScoreMapper.selectAll();
        //转vo
        List<StudentScoreVO> studentScoreVOS = new ArrayList<>();
        if (!CollectionUtils.isEmpty(studentScores)) {
            for (StudentScore studentScore : studentScores) {
                StudentScoreVO s = new StudentScoreVO();
                BeanUtils.copyProperties(studentScore, s);
                Example o = new Example(StudentScore.class);
                o.createCriteria().andEqualTo("scoreId",studentScore.getScoreId());
                studentScoreVOS.add(s);
            }
        }
        return studentScoreVOS;
    }

    /**
     * 全部学员成线
     *
     * @return
     */
    @Override
    public List<StudentScore> findAll() {
        return studentScoreMapper.selectAll();
    }
}
